<template>
  <div>
    <span style="color: grey">当前位置 >> 科研项目管理 >> 科研项目列表
    </span>

    <div class="box" style="display: inline-block">
      <div class="searchBox" style="display: flex">

        <div style="margin: 10px 0;display: flex">
          <el-input v-model="search" placeholder="输入关键字查询" style="width: 100%;" @clear="load" @keyup.enter="load"
                    clearable></el-input>
          <el-button style="margin-left: 10px" type="primary" @click="load" circle>
            <el-icon>
              <search/>
            </el-icon>
          </el-button>
        </div>
        <div style="margin: 10px 30px">
          <el-select v-model="sort" placeholder="请选择搜索类型" style="margin-left: 20px;width: 200px" @change="load">
            <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </div>
      </div>
    </div>

    <div class="block" style="margin-top: 30px" v-loading="loadering">

      <el-table :data="information" style="width: 98%" border>
        <el-table-column fixed prop="ino" label="项目编号"/>
        <el-table-column prop="iname" label="项目名称"/>
        <el-table-column prop="isource" label="项目来源"/>
        <el-table-column prop="icategory" label="项目类别"/>
        <!--        <el-table-column prop="ddepartment" label="完成单位"  />-->
        <el-table-column prop="leader" label="负责人"/>
        <!--        <el-table-column prop="member" label="课题组成员"   >
                  <template #default="scope"  >
                    <el-tag
                        v-for="o in scope.row.member.split(',')" :key="o"
                        style="margin-left: 10px;margin-top:10px;"
                        disable-transitions>
                      {{o}}
                    </el-tag>
                  </template>
                </el-table-column>-->
        <!--        <el-table-column prop="topictime" label="立项时间"  />
                <el-table-column prop="duetime" label="拟定期限"  />
                <el-table-column prop="funds" label="经费金额"  />-->

        <el-table-column prop="appraise"
                         label="鉴定状态"

        >
          <template #default="scope">
            <el-tag
                type="success"
                v-if="scope.row.appraise === '1'"
                disable-transitions>
              已鉴定
            </el-tag>

            <el-tag
                type="info"
                v-if="scope.row.appraise === '0'"
                disable-transitions>
              未鉴定
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column fixed="right" label="操作" width="200px">
          <template #default="scope">
            <el-row>
              <el-col :span="6">
                <el-button size="small" type="success" @click="handleFind(scope.row)">查看</el-button>
              </el-col>
              <el-col :span="6" style="margin-left: 10px">
                <el-button size="small" type="primary" @click="handleEdit(scope.row)">修改</el-button>
              </el-col>
              <el-col :span="6" style="margin-left: 10px">
                <el-popconfirm title="确认删除吗?" @confirm="handleDrop(scope.row)">
                  <template #reference>
                    <el-button
                        size="small"
                        type="danger">删除
                    </el-button>
                  </template>
                </el-popconfirm>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          v-model:currentPage="currentPage"
          :page-sizes="[5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          class="mt-4"
          background

      >
      </el-pagination>
    </div>

    <el-drawer
        v-model="drawer2"
        title="详情"
        :close-on-click-modal=false
    >
      <el-scrollbar height="100%">
        <el-form :model="ruleForm" status-icon ref="ruleForm" label-width="100px" class="demo-ruleForm"
                 :rules="formRules"
                 style="margin-right: 20px;"
        >
          <el-form-item label="项目编号" prop="ino">
            <el-input type="text" v-model="ruleForm.ino" autocomplete="off" @blur.prevent="changeIno()"></el-input>
            <span v-if="this.torf === false" style="font-size: 10px;color: red">该项目编号已存在</span>
          </el-form-item>

          <el-form-item label="项目名称" prop="iname">
            <el-input type="text" v-model="ruleForm.iname" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="项目来源" prop="isource">
            <el-input type="text" v-model="ruleForm.isource" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="项目类别" prop="icategory">
            <el-input type="text" v-model="ruleForm.icategory" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="完成单位" prop="ddepartment">
            <el-input type="text" v-model="ruleForm.ddepartment" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="负责人" prop="leader">
            <el-input type="text" v-model="ruleForm.leader" autocomplete="off"
                      @blur.prevent="changeLeader()"></el-input>
            <span v-if="this.torf1 === false" style="font-size: 10px;color: red">该科研人员不存在</span>
          </el-form-item>


          <el-form-item label="组员人数">
            <el-card style="width: 100%">
              <template #header>
                <div class="card-header">
                  <span>请选择课题组成员人数</span>
                </div>
              </template>
              <el-input-number v-model="memberNum" :min="1" label="ssss" @change="membersOk"></el-input-number>
            </el-card>
          </el-form-item>

          <el-form-item label="课题组成员" prop="member">
            <el-card class="box-card" style="width: 100%;">
              <template #header>
                <div class="card-header">
                  <span>单行输入一个课题组成员</span>
                  <!--              <el-button class="button" type="text" @click="membersOk">输入完成</el-button>-->
                </div>
              </template>

              <div v-for="o in this.memberNum" :key="o" class="text item">
                <el-input type="text" autocomplete="off" v-model="this.members[o]"
                          style="margin-bottom: 10px" @blur.prevent="membersOk"></el-input>
              </div>
            </el-card>
          </el-form-item>


          <el-form-item label="立项时间" prop="topictime">
            <el-date-picker v-model="ruleForm.topictime"
                            type="date" style="width: 100%" placeholder="请选择立项时间">
            </el-date-picker>
          </el-form-item>

          <el-form-item label="拟定期限" prop="duetime">
            <el-date-picker v-model="ruleForm.duetime"
                            type="date" style="width: 100%" placeholder="请选择拟定期限">
            </el-date-picker>
          </el-form-item>

          <el-form-item label="经费金额" prop="funds">
            <el-input type="text" style="width: 100%" v-model="ruleForm.funds" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="是否鉴定" prop="appraise">
            <el-switch
                v-model="ruleForm.appraise"
                active-text="已鉴定"
                inactive-text="未鉴定"
                active-value="1"
                inactive-value="0"
                @change="chagneAppraise">
            </el-switch>
          </el-form-item>

          <el-form-item label="鉴定时间" prop="appraisetime" v-if="ruleForm.appraise === '1'">
            <el-date-picker v-model="ruleForm.appraisetime"
                            type="date" style="width: 100%" placeholder="请选择鉴定时间">
            </el-date-picker>
          </el-form-item>

          <el-form-item label="鉴定单位" prop="appraisedep" v-if="ruleForm.appraise === '1'">
            <el-input type="text" v-model="ruleForm.appraisedep" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="文件资料" prop="ifiles">
            <el-upload
                class="upload-demo"
                drag
                action="http://222.21.119.252:9999/files/upload"
                multiple
                list-type="text"
                :on-success="filesUploadSuccess"
                :on-error="filesUploadError"
                :before-upload="checkFileBefore"
                :data=fileVO
                :limit="1"
                :on-exceed='limitCheck'
                :file-list="fileList"
            >
              <el-icon class="el-icon--upload">
                <upload-filled/>
              </el-icon>
              <div class="el-upload__text">
                拖拽上传 或 <em>点击上传</em>
              </div>
              <template #tip>
                <div class="el-upload__tip">
                  仅支持上传一个文件,且最大容量为100MB
                </div>
              </template>
            </el-upload>
          </el-form-item>


          <el-form-item label="备注" prop="remark">
            <el-input type="textarea"
                      :autosize="{ minRows: 2, maxRows: 4}"
                      placeholder="请输入内容" v-model="ruleForm.remark" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="editForm"
                       style="width:100%; height:100%;margin-bottom:15px;">提交
            </el-button>
          </el-form-item>
        </el-form>

      </el-scrollbar>
    </el-drawer>

    <el-drawer
        v-model="findDrower"
        title="详情"
    >
      <el-scrollbar>
        <el-descriptions :column="1" border>
          <el-descriptions-item
              label="id"
              label-align="right"
              align="center">
            {{ this.findFrom.id }}
          </el-descriptions-item>

          <el-descriptions-item label="项目编号" label-align="right" align="center"> {{ this.findFrom.ino }}
          </el-descriptions-item>

          <el-descriptions-item label="项目名称" label-align="right" align="center"> {{ this.findFrom.iname }}
          </el-descriptions-item>

          <el-descriptions-item label="项目来源" label-align="right" align="center">
            {{ this.findFrom.isource }}
          </el-descriptions-item>

          <el-descriptions-item label="项目类别" label-align="right" align="center">
            {{ this.findFrom.icategory }}
          </el-descriptions-item>


          <el-descriptions-item label="完成单位" label-align="right" align="center">
            {{ this.findFrom.ddepartment }}
          </el-descriptions-item>

          <el-descriptions-item label="负责人" label-align="right" align="center">
            {{ this.findFrom.leader }}
          </el-descriptions-item>

          <el-descriptions-item label="课题组成员" label-align="right" align="center">
            {{ this.findFrom.member }}
          </el-descriptions-item>

          <el-descriptions-item label="立项时间" label-align="right" align="center">
            {{ this.findFrom.topictime }}
          </el-descriptions-item>

          <el-descriptions-item label="拟定期限" label-align="right" align="center">
            {{ this.findFrom.duetime }}
          </el-descriptions-item>

          <el-descriptions-item label="经费金额" label-align="right" align="center">
            {{ this.findFrom.funds }}
          </el-descriptions-item>

          <el-descriptions-item label="是否鉴定" label-align="right" align="center">
            <el-tag v-if="findFrom.appraise === '1'">
              已鉴定
            </el-tag>

            <el-tag v-else type="info">
              未鉴定
            </el-tag>
          </el-descriptions-item>

          <el-descriptions-item label="鉴定时间" label-align="right" align="center" v-if="findFrom.appraise === '1'">
            {{ this.findFrom.appraisetime }}
          </el-descriptions-item>

          <el-descriptions-item label="鉴定单位" label-align="right" align="center" v-if="findFrom.appraise === '1'">
            {{ this.findFrom.appraisedep }}
          </el-descriptions-item>

          <el-descriptions-item label="文件资料" label-align="right" align="center">
            <el-popconfirm
                title="确认下载吗?"
                @confirm="downthis1(this.findFrom.ifiles)"
            >
              <template #reference>
                <el-button type="success" size="small">下载文件</el-button>
              </template>
            </el-popconfirm>
          </el-descriptions-item>

          <el-descriptions-item label="备注" label-align="right" align="center">
            {{ this.findFrom.remark }}
          </el-descriptions-item>

        </el-descriptions>
      </el-scrollbar>

    </el-drawer>
  </div>
</template>
<script>
import request from "@/utils/request";
import {Search, Edit, Delete, UploadFilled} from '@element-plus/icons-vue'

export default {
  name: "SearchItem",
  components: {
    Search,
    Edit,
    Delete,
    UploadFilled
  },
  data() {
    return {
      findFrom: {},
      fileList: [{
        name: '',
        value: ''
      }],
      fileVO: {
        sort: 'item',
        fileName: ''
      },
      memberNum: 1,
      loadering: true,
      torf: true,
      torf1: true,
      name: '',
      drawer2: false,
      findDrower: false,
      memberindex: 0,

      ruleForm: {
        iname: '',
        leader: ''
      },
      members: [],
      formRules: {
        iname: [
          {required: true, message: '请输入项目名称', trigger: 'blur'}
        ],
        appraise: [
          {required: true, message: '请选择鉴定结果', trigger: 'blur'}
        ],
        appraisedep: [
          {required: true, message: '请输入鉴定单位', trigger: 'blur'}
        ],
        appraisetime: [
          {required: true, message: '请输入鉴定时间', trigger: 'blur'}
        ],
        ddepartment: [
          {required: true, message: '请输入完成单位', trigger: 'blur'}
        ],
        duetime: [
          {required: true, message: '请选择拟定期限', trigger: 'blur'}
        ],
        funds: [
          {required: true, message: '请输入经费金额', trigger: 'blur'}
        ],
        icategory: [
          {required: true, message: '请输入项目类别', trigger: 'blur'}
        ],
        ifiles: [
          {required: true, message: '请上传文件资料', trigger: 'blur'}
        ],

        ino: [
          {required: true, message: '请输入项目编号', trigger: 'blur'}
        ],
        isource: [
          {required: true, message: '请输入项目来源', trigger: 'blur'}
        ],
        leader: [
          {required: true, message: '请输入负责人', trigger: 'blur'}
        ],
        member: [
          {required: true, message: '请点击完成课题组成员', trigger: 'blur'}
        ],
        topictime: [
          {required: true, message: '请选择立项时间', trigger: 'blur'}
        ],
      },
      options: [
        {
          value: 'Iname',
          label: '项目名称'
        },
        {
          value: 'Ino',
          label: '项目编号'
        },
        {
          value: 'Icategory',
          label: '项目类别'
        },
        {
          value: 'Leader',
          label: '负责人'
        },
        {
          value: 'Member',
          label: '课题组成员'
        },
      ],

      //下载路径
      fileUrl: '',
      //数据
      information: [],
      currentPage: 1,
      total: 0,
      search: '',
      pageSize: 10,
      sort: ''
    };
  },
  methods: {
    downthis1(file) {
      //下载文件
      window.location.href = file;
    },
    load() {
      request.get(`/item`, {
        params: {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          search: this.search,
          sort: this.sort
        }
      }).then(res => {
        if (res.code === '200') {
          this.information = res.data.records;
          this.total = res.data.total;
          this.loadering = false;
        }
      })

    },
    handleFind(row) {
      //深拷贝
      this.findFrom = JSON.parse(JSON.stringify(row));

      this.findDrower = true;
    },
    //点击表格上的修改
    handleEdit(row) {
      //深拷贝
      this.ruleForm = JSON.parse(JSON.stringify(row));
      this.name = this.ruleForm.ino;
      this.members = this.ruleForm.member.split(',');
      //因为需要第一个元素为空,即 member[0]为空元素
      this.members.unshift('');
      this.memberNum = this.members.length - 1;
      //显示文件列表
      this.fileList[0].name = '原文件';
      this.fileList[0].uri = this.ruleForm.ifiles;

      this.drawer2 = true;
    },
    //删除
    handleDrop(row) {
      //根据 主键  删除
      request.delete("/files/" + this.fileVO.sort + "/" + row.ino).then(() => {
        //无论文件是否存在都执行下一步,存在则删除,不存在则不理会,然后删除
        //删除  的 res
        request.delete("/item/" + row.id).then(res => {
          if (res.code === '200') {
            //弹出成功框
            this.$message.success("删除成功")
            this.load();
          }
        }).catch(() => {   //防止未知bug,执行不了Controller方法直接报错
              this.$message.error("服务器错误!请联系管理员或重新尝试!");
            }
        );
      }).catch(() => {
        this.$message.error("服务器错误!请联系管理员或重新尝试!");
      });
    },

//点击修改的提交按钮
    editForm() {
      this.$refs.ruleForm.validate(valid => {
        if (!valid) {
          this.$message.error("请输入必选项!")
          return
        }
        request.put("/item", this.ruleForm).then(res => {
          if (res.code === '200') {
            //弹出成功框
            this.$message.success("修改成功")
            this.load();
            this.drawer2 = false;
          } else {
            this.$message.error("修改失败!请确保表单各项填写完整正确!");
          }
        }).catch(() => {   //防止未知bug,执行不了Controller方法直接报错
          this.$message.error("服务器错误!请联系管理员或重新尝试!");
        })
      })
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.load();
    },
    handleCurrentChange(pageNum) {
      this.currentPage = pageNum;
      this.load();
    },
    //iNo 项目编号改变
    changeIno() {
      if (this.ruleForm.ino === '') {
        this.torf = true;
      } else {
        request.get(`/item/ino/` + this.ruleForm.ino).then(res => {
          if (res.code === '200') {
            //表示不重复,可以通过
            this.torf = true;
          } else {
            if (this.name === res.data.ino) {
              this.torf = true;
            } else {
              this.torf = false;
            }
          }
        });
      }
    },
    //Leader  负责人改变
    changeLeader() {
      if (this.ruleForm.leader === '') {
        this.torf1 = true;
      } else {
        request.get(`/sperson/pname/` + this.ruleForm.leader).then(res => {
          if (res.code === '200') {
            //表示没有,不能通过
            this.torf1 = false;
          } else { //表示有这个主键,再判断是不是未修改的Isbn
            if (res.data.isbn === this.isbn) {
              this.torf = true;
            } else {
              this.torf = false;
            }

          }
        });
      }
    },
    membersOk() {
      //第一个字符为 , 需要去除
      this.ruleForm.member = this.members.slice(1, this.memberNum + 1).toString();
    },
    //  修改鉴定状态
    chagneAppraise() {
      this.ruleForm.appraisetime = '';
      this.ruleForm.appraisedep = '';
    },

    //文件上传成功调用
    filesUploadSuccess(res) {
      this.ruleForm.ifiles = res.data;
    },

    //上传之前
    checkFileBefore(file) {
      if (this.ruleForm.ino === '') {
        this.$message.error("上传文件前请先输入项目编号!");
        return false;
      }
      //判断文件大小
      if (file.size / 1024 / 1024 > 100) {
        this.$message.error("文件大小不得超过100MB");
        return false;
      }
      //文件上传的参数之一
      this.fileVO.fileName = this.ruleForm.ino;
    },
    //超出文件数量限制
    limitCheck() {
      this.$message.warning('只能上传一个文件');
    },
    //上传文件失败提示
    filesUploadError() {
      this.$message.error("上传失败,请重新尝试或联系管理员");
    }
  },
  created() {
    this.load();
  }
}
</script>

<style scoped>
.box {
  margin: 0 auto;
  padding-top: 20px;
  height: 50px;
  width: 100%;
}

.searchBox {
  margin: 0 auto;
  width: 60%;
  position: relative;
}

/deep/ .el-upload .el-upload-dragger {
  width: 300px;
}
</style>
